//
//  TYCOViews.m
//  TYCollectionPictures
//
//  Created by admin on 2017/1/11.
//  Copyright © 2017年 honliv. All rights reserved.
//

#import "TYCOViews.h"
#import "TYCOCell.h"

// 每一组最大的行数
#define TYCOTotalRowsInSection (5000 * self.images.count)
#define TYCODefaultRow (NSUInteger)(TYCOTotalRowsInSection * 0.5)

#define TYCO_Width self.frame.size.width
#define TYCO_Height self.frame.size.height-30

@interface TYCOViews ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,retain) UIPageControl *pageControl;
@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic,assign) BOOL isSDWebImage;
@end

@implementation TYCOViews

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame collectionViewLayout:[self createLayoutWithFrame:frame]]) {
        
        _pageControl = [[UIPageControl alloc] init];
        //根据需要在这里调整pagecontrol的位置
        _pageControl.frame = CGRectMake((TYCO_Width-20 * _pageControl.numberOfPages)*0.5, TYCO_Height, 20 * _pageControl.numberOfPages, 20);
        _pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
        _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
        self.pagingEnabled = YES;
        self.showsHorizontalScrollIndicator = NO;
        self.delegate = self;
        self.dataSource = self;
        self.bounces = NO;
        self.backgroundColor = [UIColor whiteColor];
        _isSDWebImage = NO;
    }
    return self;
}

-(UICollectionViewLayout *)createLayoutWithFrame:(CGRect)frame{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.minimumLineSpacing = 0;
    layout.itemSize = frame.size;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    return layout;
}
-(void)setCO_Interval:(CGFloat)CO_Interval{
    _CO_Interval = CO_Interval;
    [self addTimer];
}
- (void)removeTimer{
    [self.timer invalidate];
    self.timer = nil;
}
#pragma mark -- 添加定时器
- (void)addTimer{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:_CO_Interval target:self selector:@selector(nextNews) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
-(void)nextNews{
    NSIndexPath *visiablePath = [[self indexPathsForVisibleItems] firstObject];
    NSUInteger visiableItem = visiablePath.item;
    if ((visiableItem % self.images.count) == 0) {
        [self scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:TYCODefaultRow inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
        visiableItem = TYCODefaultRow;
    }
        NSUInteger nextItem = visiableItem + 1;
        [self scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:nextItem inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}
#pragma mark -- 设置图片资源
-(void)setImages:(NSArray *)images{
    _images = images;
    _pageControl.numberOfPages = self.images.count;
    [self reloadData];
    [self scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:TYCODefaultRow inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    //解决复用时pageControl的短暂消失重现
    dispatch_async(dispatch_get_main_queue(), ^{
        [self addpageControl];
    });
}
#pragma mark -- 这是网络图片
-(void)setSD_images:(NSArray *)SD_images{
    _isSDWebImage = YES;
    [self setImages:SD_images];
}
#pragma mark -- 添加pageControl
-(void)addpageControl{
    [[self superview] addSubview:_pageControl];
}
#pragma mark -- 数据源

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return TYCOTotalRowsInSection;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TYCOCell *cell = [TYCOCell TYCOCellWithCollectionView:collectionView indexPath:indexPath];
    if (!_isSDWebImage) {
        NSDictionary *dic = self.images[indexPath.item % self.images.count];
        cell.icon = dic[@"icon"];
    }else{
        NSString *iconUrl = self.images[indexPath.item % self.images.count];
        cell.iconUrl = iconUrl;
    }
//    [cell addSubview:_pageControl];
    cell.bitmapImage = self.bitmapImage;
    return cell;
}
#pragma mark -- 滑动
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger index = indexPath.item % self.images.count;
    _pageControl.currentPage = index;
}

//手势拖拽
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self removeTimer];
}

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    [self addTimer];
}

//点击的方法
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    if ([self.TYDelegate respondsToSelector:@selector(TYCOViewClickedImageAtItem:)]) {
        [self.TYDelegate TYCOViewClickedImageAtItem:self.pageControl.currentPage];
    }
}

@end
